package MeteoCal.bean;

import MeteoCal.entity.Event;
import MeteoCal.manager.UserManager;
import MeteoCal.entity.User;
import java.io.IOException;
import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @authors Guttadoro, Iommazzo, Saullo
 */
@ManagedBean(name="userBean")
@SessionScoped
public class UserBean implements Serializable{

    @EJB
    UserManager um;
    
    private Date date1;
    private String newPassword;
    private String passwordControl;
    List<User> users;
    private User userup;
    private boolean publicCalendar;

    public UserBean() {
    }
    
    public String getNewPassword() {
        return newPassword;
    }

    public void setnewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public UserManager getUm() {
        return um;
    }

    public void setUm(UserManager um) {
        this.um = um;
    }

    public boolean isPublicCalendar() {
        return publicCalendar;
    }

    public void setPublicCalendar(boolean publicCalendar) {
        this.publicCalendar = publicCalendar;
    }
    
    public String getName(){
        return userup.getName();
    }
    
    public void setName(String name) {
        this.userup.setName(name);
    }
    
    public String getPasswordControl() {
        return passwordControl;
    }

    public void setPasswordControl(String passwordControl) {
        this.passwordControl = passwordControl;
    }
    
    public Date getDate1() {
        return date1;
    }
 
    public void setDate1(Date date1) {
        this.date1 = date1;
    }

    public User getUserup() {
        return userup;
    }

    public void setUserup(User userup) {
        this.userup = userup;
    }
    
    public String getName2() {
        return um.getLoggedUser().getName();
    }

    public User getUser() {
        return um.getLoggedUser();
    }
    
    public Collection<Event> showEventsDay(){
        
        return um.getEventDay(this.getToday());
        
    }
     
    private Date getToday() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }
    
    public String notifications(){
        return "notification?faces-redirect=true";
    }
    
    public String updateProfile(){
    
        userup = um.getLoggedUser();
        return "updateprofile?faces-redirect=true";
    
    }
    
    public String changeCalendar(){
        User u = um.getLoggedUser();
        u.getIdSchedule().setPublic1(this.publicCalendar);
        um.saveUser(u);
        return "homepage?faces-redirect=true";
    }
    
    public String update() { // getlogged user su tutti :(
        um.update(userup);
        return "home?faces-redirect=true"; //edit dovrebbe funzionare ma non funziona
    }
      
    public String changePassword() throws  NoSuchAlgorithmException, IOException { // getlogged user su tutti :(
           
        System.out.println(sha256(this.passwordControl)+ " " + getUser().getPassword() + "new " + newPassword + "SHA --> " + sha256(newPassword));
        if (sha256(this.passwordControl).equals(getUser().getPassword())){
            getUser().setPassword(sha256(newPassword));
            um.update(getUser());
        }
        return "user/home?faces-redirect=true";
    }
    
    public String icon() throws NoSuchAlgorithmException, IOException{

        
        if (passwordControl != null && (sha256(passwordControl)).equals(getUser().getPassword())){
                return "https://cdn2.iconfinder.com/data/icons/basicset/tick_64.png";
        }
        return "http://www.hcdpergamino.gob.ar/mysite/images/no-foto.png";
    }
    
    public String sha256(String pass) {
        try{
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(pass.getBytes("UTF-8"));
            StringBuffer hexString = new StringBuffer();

            for (int i = 0; i < hash.length; i++) {
                String hex = Integer.toHexString(0xff & hash[i]);
                if(hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }

            return hexString.toString();
        } catch(Exception ex){
            throw new RuntimeException(ex);
        }
    }
    
    public String goToProfile(){
        return "profile?face-redirect=true";
    }
}
